package com.newworld.dmsapp2.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.activity.HistorySearchActivity;
import com.newworld.dmsapp2.activity.HouseManageListActivity;
import com.newworld.dmsapp2.activity.ToDoTaskListActivity;
import com.newworld.dmsapp2.activity.serviceapply.WriteOutdoorPaperActivity;
import com.newworld.dmsapp2.activity.serviceapply.WritePaperActivity;
import com.newworld.dmsapp2.bean.Function;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.fragment.adapter.HomeMenuItemAdapter;
import com.newworld.dmsapp2.fragment.bean.MenuItem;
import com.newworld.dmsapp2.ui.view.PublicNoticeView;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.SingleSample;

public class MainHomeFragment extends Fragment implements ActionBar.TabListener{
	Resources resources;
    private ViewPager mPager;
    private ImageView ivBottomLine;
    private TextView tvTabNew, tvTabHot;
	ActionBar actionBar;
	private LinearLayout blayout;
	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	public final static int num = 2 ; 
	Fragment home1;
	Fragment home2;
	private ArrayList<Fragment> fragmentsList;
	private LinearLayout func_ly1,func_ly2;
	
	private GridView home_grid;
	@SuppressWarnings("unused")
	private PublicNoticeView noticeTv;
	Context context;
	private Bundle bundle = new Bundle();
	private Intent intent;
	private User user;
	private List<Function> functions;
	final List<MenuItem> menus = new ArrayList<MenuItem>();
	private List<String> functionCodeList = new ArrayList<String>();
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main_home, container,
				false);
		findView(view);
		init();
		initView(view);
		initEvent();
		return view;
	}
	private void findView(View v) {
		//home_grid = (GridView) v.findViewById(R.id.home_grid);
		noticeTv = (PublicNoticeView) v
				.findViewById(R.id.mainfragment_notice_tv);
		noticeTv.setVisibility(View.GONE);
	}
	
	private void initView(View parentView){
		resources = getResources();
		ivBottomLine = (ImageView) parentView.findViewById(R.id.iv_bottom_line);
		bottomLineWidth = ivBottomLine.getLayoutParams().width;
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int avg = 0 ;
		if(Config.FUNCTION_PAGE == 1){
			offset = (int) ((screenW- bottomLineWidth)/ num);
			 avg = (int) (screenW / num);
		}else if(Config.FUNCTION_PAGE == 2){
			offset = (int) ((screenW / num - bottomLineWidth) / 2);
			 avg = (int) (screenW / num);
		}
		
		position_one = avg + offset;
		mPager = (ViewPager) parentView.findViewById(R.id.pager);
		fragmentsList = new ArrayList<Fragment>();
		if (Config.FUNCTION_PAGE == 1) {
			func_ly1 = (LinearLayout)parentView.findViewById(R.id.func_ly1);
			tvTabNew = (TextView) parentView.findViewById(R.id.tv_tab_1);
			tvTabNew.setTextColor(resources.getColor(R.color.nw_bt_blue));
			tvTabNew.setOnClickListener(new MyOnClickListener(0));
			home1 = new MainHomeFragmentPiece();
			fragmentsList.add(home1);
			/*func_ly2 = (LinearLayout)parentView.findViewById(R.id.func_ly2);
			func_ly2.setVisibility(View.GONE);*/
			//judgePageShow();
		} else if (Config.FUNCTION_PAGE == 2) {
			func_ly1 = (LinearLayout)parentView.findViewById(R.id.func_ly1);
			tvTabNew = (TextView) parentView.findViewById(R.id.tv_tab_1);
			tvTabNew.setTextColor(resources.getColor(R.color.nw_bt_blue));
			tvTabNew.setOnClickListener(new MyOnClickListener(0));
			home1 = new MainHomeFragmentPiece();
			fragmentsList.add(home1);
			func_ly2 = (LinearLayout)parentView.findViewById(R.id.func_ly2);
			func_ly2.setVisibility(View.VISIBLE);
			tvTabHot = (TextView) parentView.findViewById(R.id.tv_tab_2);
			tvTabHot.setTextColor(resources.getColor(R.color.nw_edit_gray));
			tvTabHot.setOnClickListener(new MyOnClickListener(1));
			home2 = new MainHomeFragmentPieceNext();
			fragmentsList.add(home2);
			//judgePageShow();
		}
		mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(),
				fragmentsList));
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
		mPager.setCurrentItem(0);
	}
	
	private void initEvent(){
		TranslateAnimation animation = new TranslateAnimation(position_one, offset, 0, 0);
       /* tvTabNew.setTextColor(resources.getColor(R.color.nw_bt_blue));
        tvTabHot.setTextColor(resources.getColor(R.color.nw_edit_gray));*/
        animation.setFillAfter(true);
        animation.setDuration(300);
        ivBottomLine.startAnimation(animation);
	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, offset, 0,
							0);
					tvTabHot.setTextColor(resources
							.getColor(R.color.nw_bg_gray));
				}
				tvTabNew.setTextColor(resources.getColor(R.color.nw_bt_blue));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, position_one, 0,
							0);
					tvTabNew.setTextColor(resources
							.getColor(R.color.nw_bg_gray));
				}
				tvTabHot.setTextColor(resources.getColor(R.color.nw_bt_blue));
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	private void init() {
		
		functionCodeList.clear();
		user = SingleSample.getInstance(getActivity()).getUser();
		functions = user.getFunctions();
		Log.e("functions", functions.toString());
		menus.clear();
		for (Function function : functions) {
			functionCodeList.add(function.getFunctionCode());
		}
		Config.FUNCTION_NUM = 0;
		if (functionCodeList.contains(Config.FUNCTION_REPAIRALLY)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_REPAIRPROGRESS)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_HOUSEMANAGE)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_REPAIRTASK)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_REPAIRDISTRIBUTE)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_RESSIGNMENT)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_MANAGERDISPATCH)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_MANAGERREDISPATCH)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_OUTTIMEREPAIR)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_RETURNVISIT)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_HISTORYQUERY)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_GET_WORKER_HISTORYLIST)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_PMREVISIT)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_OUTDOORREPAIR)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_MYAPPLY)) {
			Config.FUNCTION_NUM++;
		}
		if (functionCodeList.contains(Config.FUNCTION_OUTTIMEREPAIROFDEPART)) {
			Config.FUNCTION_NUM++;
		}
		if (true) {// 更多
			Config.FUNCTION_NUM++;
		}
		System.out.println("functionCodeList---:"+functionCodeList.toString());
		System.out.println("functionCodeList.size():"+functionCodeList.size());
		//判断functioncode显示需要的页数
		//int func_page = Config.FUNCTION_NUM/6 ;
		if(Config.FUNCTION_NUM < 6||Config.FUNCTION_NUM == 6){
			Config.FUNCTION_PAGE = 1;
		} else if(Config.FUNCTION_NUM > 6){
			Config.FUNCTION_PAGE = 2 ;
		}
		context = this.getActivity();
		
	} 

	@Override
	public void onTabUnselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction)
	{
	}

	// 当指定Tab被选中时激发该方法
	@Override
	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction)
	{
		mPager.setCurrentItem(tab.getPosition());  //②
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction)
	{
	}
}
